﻿@{
    ViewBag.Title = "Quản lý sản phẩm";
    var debugger = (Dictionary<String, String>)ViewData["debugger"];
}
@section JavaScript
{  
    <script src="~/Content/vallumn/fileuploader.js" type="text/javascript"></script>
}
@section Styles
{    
    <link href="~/Content/vallumn/fileuploader.css" rel="stylesheet" type="text/css" />
}

@if (Roles.IsUserInRole("Debugger"))
{
	<div class="box">
		<h2 class="head-big-title">
			<a href="#"><span>Chế độ demo truy xuất đồng thời</span></a>
		</h2>
        <div class="sub_content" style="height: 54px;">
              @using (Html.BeginForm("Index", "Product", FormMethod.Post))
              {
                  <br/>
                  <span>Chọn chế độ:</span>
                  <select name="product_debugger" onchange="this.form.submit()">
                      @foreach (var d in debugger)
                      {
                          <option @(Session["product_debugger"].Equals(d.Key) ? "selected" : "") 
                              value="@d.Key">@d.Value</option>
                      }
                  </select>
              }
        </div>
    </div>
}

<div class="box">
    <h2 class="head-big-title">
        <a href="#"><span>Quản lý danh sách sản phẩm</span></a>
    </h2>
    <div class="sub_content">
        <div id="ProductTable" style="width: 100%; margin: auto;"></div>
    </div>
</div>
<script type="text/javascript">

    $(document).ready(function () {
        var editorIntro;
        var editorContent;
        //Prepare jtable plugin
        $('#ProductTable').jtable({
            paging: true, //Enable paging
            pageSize: 10, //Set page size (default: 2)
            //sorting: true, //Enable sorting
            //defaultSorting: 'Name Id', //Set default sorting
            actions: {
                listAction: '/Admincp/Product/GetLists',
                deleteAction: '/Admincp/Product/Delete',
                updateAction: '/Admincp/Product/Update',
                createAction: '/Admincp/Product/Create'
            },
            fields: {
                PromotionDetail: { 
                    title: 'Khuyến mãi',
                    width: '10%',
                    sorting: false,
                    edit: false,
                    create: false,
                    display: function (product) {
                        //Create an image that will be used to open child table
                        var $img = $('<img src="/Content/images/note.png" title="Chỉnh sửa danh sách khuyến mãi" />');
                        //Open child table when user clicks the image
                        $img.click(function () {
                            $('#ProductTable').jtable('openChildTable',
                                    $img.closest('tr'),
                                    {
                                        title: 'Sản phẩm khuyến mãi của sản phẩm: '+product.record.Name,
                                        actions: {
                                            listAction: '/Admincp/ProductPromotion/GetLists?ProductId=' + product.record.Id,
                                            deleteAction: '/Admincp/ProductPromotion/Delete',
                                            updateAction: '/Admincp/ProductPromotion/Update',
                                            createAction: '/Admincp/ProductPromotion/Create'
                                        },
                                        fields: {
                                            ProductId: {
                                                type: 'hidden',
                                                defaultValue: product.record.Id
                                            },
                                            Id: {
                                                title: 'Mã SPKM',
                                                key: true,
                                                create: false,
                                                edit: false,
                                            },
                                            Name: {
                                                title: 'Tên SP Khuyến mãi',
                                                width: '30%'
                                            },
                                            GiftPointRequired: {
                                                title: 'Điểm yêu cầu',
                                            },
                                            MaxGiftAmount: {
                                                title: 'SL nhận tối đa',
                                            },
                                            Quantity: {
                                                title: 'Số lượng',
                                            },
                                            RemainAmount: {
                                                title: 'Số lượng còn',
                                                type: 'readonly'
                                            },
                                            GiftOfferedCount: {
                                                 title: 'Đã tặng',
                                                 defaultValue: 0,
                                                 create: false,
                                                 edit: false,
                                            }
                                        }
                                    }, function (data) { //opened handler
                                        data.childTable.jtable('load');
                                    });
                        });
                        //Return image to show on the person row
                        return $img;
                    }
                },
                Id: {
                    title: "Mã SP",
                    key: true,
                    create: false,
                    edit: false,
                    width:'5%'
                },
                Name: {
                    title: 'Tên SP',
                },
                ProductType_Id: {
                    title: 'Loại SP',
                    options: '/Admincp/Product/GetProductTypes'
                },
                Price: {
                    title: 'Giá',
                    display: function (data) {
                        return accounting.formatNumber(data.record.Price,0,",")+'<sup>đ</sup>';
                    }
                },
                Quantity: {
                    title: 'Số lượng',
                    display: function (data) {
                        return data.record.RemainAmount + '/' + data.record.Quantity;
                    }
                },
               
                RemainAmount:  {
                    title: 'Số lượng tồn',
                    list: false
                },
            
                GiftPoint: {
                    title: 'Điểm'
                },
                StartSaleDate: {
                    title: 'BĐầu bán',
                    type: 'date',
                    displayFormat: 'dd-mm-yy'
                },
                
                EndSaleDate: {
                    title: 'KThúc bán',
                    type: 'date',
                    displayFormat: 'dd-mm-yy'
                   
                },  
                StartReceiveDate: {
                    title: 'BĐầu nhận',
                    type: 'date',
                    displayFormat: 'dd-mm-yy'
                },
                
                EndReceiveDate: {
                    title: 'KThúc nhận',
                    type: 'date',
                    displayFormat: 'dd-mm-yy'
                   
                },
                DateAdded: {
                    title: 'Ngày thêm',
                    type: 'date',
                    displayFormat: 'dd-mm-yy',
                    create: false,
                    list:false,
                    edit:false
                },
                Image:  {
                    title: 'Hình ảnh',
                    type: 'image',
                    src: '/Content/images/products/',
                    list: false
                },
                Intro:  {
                    title: 'Giới thiệu',
                    edit: true,
                    type: 'textarea',
                    list: false
                },
                Contents:  {
                    title: 'Nội dung',
                    type: 'textarea',
                    list: false
                },
            },
            formCreated: function (event, data) {
                // Validate controls
                data.form.find('input[name="Name"]').addClass(
                  'validate[required]');
                data.form.find('input[name="ProductType_Id"]').addClass(
                  'validate[required]');
                data.form.find('input[name="Price"]').addClass(
                  'validate[required,custom[number]]');
                data.form.find('input[name="Quantity"]').addClass(
                  'validate[required,custom[integer]]');
                data.form.find('input[name="RemainAmount"]').addClass(
                  'validate[required,custom[integer]]');
                data.form.find('input[name="GiftPoint"]').addClass(
                  'validate[required,custom[integer]]');
                data.form.find('input[name="StartSaleDate"]').addClass(
                  'validate[required,custom[date]]');
                data.form.find('input[name="EndSaleDate"]').addClass(
                  'validate[required,custom[date]]');
                data.form.validationEngine();
 
                
                // Khởi tạo trình soạn thảo
                editorIntro = CKEDITOR.replace('Intro');
                editorContent = CKEDITOR.replace('Contents');
                // Khởi tạo upload ảnh
                var uploader = new qq.FileUploader({
                element: document.getElementById('Image'),
                action: '/Admincp/Product/Upload',
                allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],   
                debug: true,
                multiple: false,
                onComplete: function(file, response) {
                    $('#Img_Image').attr('src','/Content/images/products/'+response);
                    $('input[name="Image"]').val(response);
                }
            });
                
            },
            formSubmitting: function (event, data) {
                // Set content editor to textarea
                $('textarea[name=Intro]').val(editorIntro.getData());
                $('textarea[name=Contents]').val(editorContent.getData());
                return data.form.validationEngine('validate');
            },
            formClosed: function (event, data) {
                data.form.validationEngine('hide');
                data.form.validationEngine('detach');
                // destroy editor
                editorIntro.destroy();
	            editorIntro = null;
                editorContent.destroy();
                editorContent = null;
            }
        });

        //Load ProductTable list from server
        $('#ProductTable').jtable('load');
        
    });
    
</script>
